﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ET.DBCommon.Model;
using MySql.Data.MySqlClient;

namespace ET.DBCommon.DAL
{
    public class DeviceDAL
    {
        public static List<DeviceType> GetDeviceTypeList()
        {
            string sql = "SELECT * FROM device_type";
            DataSet ds = DbHelper.ExecuteDataset(sql);
            List<DeviceType> lst = new List<DeviceType>();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DeviceType info = new DeviceType();
                    info.TypeName = dt.Rows[i]["DeviceType"].ToString();
                    info.ID = int.Parse(dt.Rows[i]["Id"].ToString());
                    lst.Add(info);
                }
            }
            return lst;
        }
        /// <summary>
        /// 获取设备类型
        /// </summary>
        /// <param name="devTypeID">设备的ID</param>
        /// <param name="devName">设备的名称</param>
        /// <returns></returns>
        public static List<Device> GetDeviceByType(int devTypeID, string devName = "")
        {
            string sql = "SELECT * FROM device where DeviceTypeId=" + devTypeID;
            if (devName != string.Empty)
            {
                sql += " and DevName='" + devName + "'";
            }
            DataSet ds = DbHelper.ExecuteDataset(sql);
            List<Device> lst = new List<Device>();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Device info = new Device();
                    info.Id = int.Parse(dt.Rows[i]["Id"].ToString());
                    info.DeviceName = dt.Rows[i]["DeviceName"].ToString();
                    info.DeviceTypeId = int.Parse(dt.Rows[i]["DeviceTypeId"].ToString());
                    info.DeviceId = int.Parse(dt.Rows[i]["DeviceId"].ToString());
                    info.SysType = dt.Rows[i]["SysType"].ToString();
                    lst.Add(info);
                }
            }
            return lst;
        }

        public static List<Device> GetDeviceList(string externSql="")
        {
            string sql = "SELECT * FROM device " + externSql;
            DataSet ds = DbHelper.ExecuteDataset(sql);
            List<Device> lst = new List<Device>();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Device info = new Device();
                    info.DeviceName = dt.Rows[i]["DeviceName"].ToString();
                    info.DeviceTypeId = int.Parse(dt.Rows[i]["DeviceTypeId"].ToString());
                    info.DevType1 = int.Parse(dt.Rows[i]["DeviceTypeId"].ToString());
                    //info.ID = int.Parse(dt.Rows[i]["ID"].ToString());
                    info.DeviceId = int.Parse(dt.Rows[i]["DeviceId"].ToString());
                    lst.Add(info);
                }
            }
            return lst;
        }

        public static List<DeviceStateModel> GetDeviceStateList()
        {
            string sql = "SELECT * FROM device_state";
           DataSet ds = DbHelper.ExecuteDataset(sql);
            List<DeviceStateModel> lst = new List<DeviceStateModel>();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DeviceStateModel info = new DeviceStateModel();
                    info.DeviceStatus = dt.Rows[i]["DeviceStatus"].ToString();
                    info.DeviceTypeId = int.Parse(dt.Rows[i]["DeviceTypeId"].ToString());
                    info.ID = int.Parse(dt.Rows[i]["Id"].ToString());
                    lst.Add(info);
                }
            }
            return lst;
        }

        /// <summary>
        /// 折返进路和引导进路区分
        /// </summary>
        /// <param name="deviceID">设备元素ID</param>
        /// <returns>返回元素类型的ID</returns>
        public static int getDeviceTypeID(int deviceID)
        {
            int deviceTypeID = 0;
            string sql = "select DT.ID from device as D " +
                "inner join device_type as DT on DT.ID=D.DevType " +
                "where D.DeviceID={0} and DT.ID in (2,3)";
            sql = string.Format(sql, deviceID);
            DataSet ds = DbHelper.ExecuteDataset(sql);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                deviceTypeID = (int)ds.Tables[0].Rows[0]["ID"];
            }
            return deviceTypeID;
        }
    }
}
